package com.bgs.service.impl;

import com.bgs.common.R;
import com.bgs.pojo.Comments;
import com.bgs.mapper.CommentsMapper;
import com.bgs.pojo.vo.Commentss;
import com.bgs.pojo.vo.ZeroComments;
import com.bgs.pojo.vo.ZeroLike;
import com.bgs.service.CommentsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Service
public class CommentsServiceImpl extends ServiceImpl<CommentsMapper, Comments> implements CommentsService {
    @Resource
    private CommentsMapper commentsMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public R zeroCircleIdToFindComments(Integer circleId, Integer beforeUserId) {
        List<ZeroComments> list = commentsMapper.zeroCircleIdToFindComments(circleId);
        for (int i = 0; i < list.size(); i++) {
            //评论id与登录用户id存在
            Double score1 = redisTemplate.opsForZSet().score("CommentsId" + list.get(i).getCommentsId(), beforeUserId);
            ZeroLike zeroLike1 = new ZeroLike();
            zeroLike1.setLikeId(null);
            zeroLike1.setLikeLength(null);
            if (score1 != null) {
                zeroLike1.setLikeId(true);
                list.get(i).setZeroLike(zeroLike1);
            } else {
                zeroLike1.setLikeId(false);
                list.get(i).setZeroLike(zeroLike1);
            }
            Set set1 = redisTemplate.opsForZSet().rangeWithScores("CommentsId" + list.get(i).getCommentsId(), 0, -1);
            if (set1 != null) {
                zeroLike1.setLikeLength(set1.size());
                list.get(i).setZeroLike(zeroLike1);
            }
            List<Commentss> commentss = commentsMapper.zeroCommentsIdToFindComments(list.get(i).getCommentsId());
            for (int j = 0; j < commentss.size(); j++) {

                Double score2 = redisTemplate.opsForZSet().score("CommentsId" + commentss.get(j).getCommentsId(), beforeUserId);
                ZeroLike zeroLike2 = new ZeroLike();
                zeroLike2.setLikeId(null);
                zeroLike2.setLikeLength(null);
                if (score2 != null) {
                    zeroLike2.setLikeId(true);
                    commentss.get(j).setZeroLike(zeroLike2);
                } else {
                    zeroLike2.setLikeId(false);
                    commentss.get(j).setZeroLike(zeroLike2);
                }
                Set set2 = redisTemplate.opsForZSet().rangeWithScores("CommentsId" + commentss.get(j).getCommentsId(), 0, -1);
                if (set2 != null) {
                    zeroLike2.setLikeLength(set2.size());
                    commentss.get(j).setZeroLike(zeroLike2);
                }
            }
            list.get(i).setCommentsList(commentss);
        }
        return R.ok().data("list", list);
    }

    @Override
    public R zeroEvaluateIdToFindComments(Integer evaluateId) {
        List<ZeroComments> list = commentsMapper.zeroEvaluateIdToFindComments(evaluateId);
        for (int i = 0; i < list.size(); i++) {
            List<Commentss> commentss = commentsMapper.zeroCommentsIdToFindComments(list.get(i).getCommentsId());
            list.get(i).setCommentsList(commentss);
        }
        return R.ok().data("list", list);
    }

    @Override
    public R zeroAddCircleIdComments(Comments comments) {
        boolean b = commentsMapper.zeroAddCircleIdComments(comments);
        return R.ok().data("b", b);
    }

    @Override
    public R zeroAddEvaluateIdComments(Comments comments) {
        boolean b = commentsMapper.zeroAddEvaluateIdComments(comments);
        return R.ok().data("b", b);
    }

    @Override
    public R zeroDeleteCommentsId(Integer commentsId) {
        boolean b = commentsMapper.zeroDeleteCommentsId(commentsId);
        return R.ok().data("b", b);
    }

    @Override
    public R zerobeforeUserIdToFindComments(Integer beforeUserId) {
        List<Comments> list = commentsMapper.zerobeforeUserIdToFindComments(beforeUserId);
        return R.ok().data("list", list);
    }

    @Override
    public R zeroOrderDetailIdToFindComments(Integer orderDetailId, Integer beforeUserId) {
        List<ZeroComments> list = commentsMapper.zeroOrderDetailIdToFindComments(orderDetailId);
        for (int i = 0; i < list.size(); i++) {
            //评论id与登录用户id存在
            Double score1 = redisTemplate.opsForZSet().score("CommentsId" + list.get(i).getCommentsId(), beforeUserId);
            ZeroLike zeroLike1 = new ZeroLike();
            zeroLike1.setLikeId(null);
            zeroLike1.setLikeLength(null);
            if (score1 != null) {
                zeroLike1.setLikeId(true);
                list.get(i).setZeroLike(zeroLike1);
            } else {
                zeroLike1.setLikeId(false);
                list.get(i).setZeroLike(zeroLike1);
            }
            Set set1 = redisTemplate.opsForZSet().rangeWithScores("CommentsId" + list.get(i).getCommentsId(), 0, -1);
            if (set1 != null) {
                zeroLike1.setLikeLength(set1.size());
                list.get(i).setZeroLike(zeroLike1);
            }
            List<Commentss> commentss = commentsMapper.zeroCommentsIdToFindComments(list.get(i).getCommentsId());
            for (int j = 0; j < commentss.size(); j++) {
                Double score2 = redisTemplate.opsForZSet().score("CommentsId" + commentss.get(j).getCommentsId(), beforeUserId);
                ZeroLike zeroLike2 = new ZeroLike();
                zeroLike2.setLikeId(null);
                zeroLike2.setLikeLength(null);
                if (score2 != null) {
                    zeroLike2.setLikeId(true);
                    commentss.get(j).setZeroLike(zeroLike2);
                } else {
                    zeroLike2.setLikeId(false);
                    commentss.get(j).setZeroLike(zeroLike2);
                }
                Set set2 = redisTemplate.opsForZSet().rangeWithScores("CommentsId" + commentss.get(j).getCommentsId(), 0, -1);
                if (set2 != null) {
                    zeroLike2.setLikeLength(set2.size());
                    commentss.get(j).setZeroLike(zeroLike2);
                }
            }
            list.get(i).setCommentsList(commentss);
        }
        return R.ok().data("list", list);
    }

    @Override
    public R zeroAddOrderDetailIdComments(Comments comments) {
        boolean b = commentsMapper.zeroAddOrderDetailIdComments(comments);
        return R.ok().data("b", b);
    }
}
